<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class ProductionType extends BaseModel
{
    use HasFactory;


    const CREATED_AT='create_time';
    const UPDATED_AT='change_time';

    protected $table = 'production_type';


     /**
     *  作品类型添加
     * @param $data 添加的数据
     */
    public function add($data){
        $this->name = $data->name;
        $this->intro = $data->intro;
        $this->is_del = 1;

        return  $this->save();
    }

    /**
     *  作品类型修改
     * @param $data 添加的数据
     */
    public function change($data){
        $res = $this->where('is_del' , 1)->find($data['id']);
        if(!$res){
            return false;
        }

        $res->name = $data->name;
        $res->intro = $data->intro;
        return  $res->save();
    }


    /**
     * 判断类型是否已经存在
     * @param name 前言名称
     * @param id 前言id   可选，主要是用于修改
     */
    public function productionTypeNameIsExists($name , $id = null){
        $res = $this->where('name' , $name)->where(function($query) use($id){
                if(!empty($id)){
                    $query->where('id' , '<>' , $id);
                }
        })->where('is_del' , 1)->first();
        return $res;
    }

}
